package com.whuan.student.controller;

import com.whuan.student.VO.ResultVO;
import com.whuan.student.dataObject.PageObject;
import com.whuan.student.dataObject.Student;
import com.whuan.student.dto.StudentDTO;
import com.whuan.student.enums.ResultEnum;
import com.whuan.student.exceptions.StudentException;
import com.whuan.student.service.StudentService;
import com.whuan.student.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class StudentController {

    public static Integer i =1;

    @Autowired
    private StudentService studentService;

    @GetMapping("/findById")
    public ResultVO findById(@RequestParam String StudentId){
        Student student = studentService.findById(StudentId);
        return ResultVOUtil.success(student);
    }
    @GetMapping("/findOne")
    public StudentDTO findOne(@RequestParam String StudentId){
        log.info(new Date().toString());
        try {
            Thread.sleep(12000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info(new Date().toString());
        Student student = studentService.findById(StudentId);
        // job加一
        student.setJob(student.getJob()+ i);
        StudentDTO studentDTO = new StudentDTO();
        BeanUtils.copyProperties(student,studentDTO);
        Student save = studentService.save(studentDTO);
        return studentDTO;
    }

    @GetMapping("/findByUpClass")
    public ResultVO findByUpClass(){
        List<Student> students = studentService.findByUpClass();
        return ResultVOUtil.success(students);
    }
    @PostMapping("/findByUpClassWithPage")
    public ResultVO findByUpClassWithPage(@RequestBody PageObject pageObject){
        log.info("pageObject={}",pageObject);
        Page<Student> students = studentService.findByUpClassWithPage(pageObject);
        log.info("students.getTotalElements()={}",students.getTotalElements());
        log.info("students.getTotalElements()={}",students.getTotalPages());
        log.info("students.getTotalElements()={}",students.getContent());
        return ResultVOUtil.success(students);
    }
    @GetMapping("/findAll")
    public ResultVO findAll(){
        List<Student> students = studentService.findAll();
        return ResultVOUtil.success(students);
    }
    @PostMapping("/saveStudent")
    public ResultVO saveStudent(@RequestBody StudentDTO studentDTO ,BindingResult bindingResult, HttpServletResponse response){
        log.info("studentDTO={}",studentDTO);
        //判断表单验证是否成功
        if (bindingResult.hasErrors()) {
            log.error(" studentDTO={}", studentDTO);
            throw new StudentException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        Student student = studentService.save(studentDTO);

        return ResultVOUtil.success(student);
    }

    @PostMapping("/batchSaveStudent")
    public ResultVO batchSaveStudent(@RequestBody List<StudentDTO> studentDTOList , HttpServletResponse response){
        log.info("studentDTOList={}",studentDTOList);
        log.info("studentDTOList.size={}",studentDTOList.size());

        Integer save = studentService.save(studentDTOList);

        return ResultVOUtil.success(save);
    }
/*    @PostMapping("/updateStudent")
    public ResultVO saveStudent(@RequestBody StudentDTO studentDTO ,BindingResult bindingResult, HttpServletResponse response){
        log.info("studentDTO={}",studentDTO);
        //判断表单验证是否成功
        if (bindingResult.hasErrors()) {
            log.error(" studentDTO={}", studentDTO);
            throw new StudentException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        Student student = studentService.save(studentDTO);

        return ResultVOUtil.success(student);
    }*/


}
